package net.top.viewer.components
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.Image;
	import mx.managers.IFocusManagerComponent;
	
	import spark.components.supportClasses.SkinnableComponent;
	
	[SkinState("normal")]
	[SkinState("selected")]
	
	public class TitlebarButton extends SkinnableComponent implements IFocusManagerComponent
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  icon
		//----------------------------------
		
		[SkinPart(required="false")]
		public var icon:Image;
		
		//----------------------------------
		//  source
		//----------------------------------
		
		[Bindable]
		public var source:Object;
		
		//----------------------------------
		//  callback
		//----------------------------------
		
		public var callback:Function;
		
		//----------------------------------
		//  selectable
		//----------------------------------
		
		public var selectable:Boolean = true;
		
		//----------------------------------
		//  selected
		//----------------------------------
		
		private var _selected:Boolean = false;
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function set selected(value:Boolean):void
		{
			if (selectable && _selected != value)
			{
				_selected = value;
				invalidateSkinState();
			}
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Skin Management
		//
		//--------------------------------------------------------------------------
		
		override protected function getCurrentSkinState():String
		{
			return selected ? "selected" : "normal";
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (instance == icon)
			{
				icon.addEventListener(MouseEvent.CLICK, icon_clickHandler);
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			if (instance == icon)
			{
				icon.removeEventListener(MouseEvent.CLICK, icon_clickHandler);
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Focus Management
		//
		//--------------------------------------------------------------------------
		
		private var keyboardPressed:Boolean = false;
		
		override protected function keyDownHandler(event:KeyboardEvent):void
		{
			if (event.keyCode != Keyboard.SPACE)
			{
				return;
			}
			
			keyboardPressed = true;
			event.updateAfterEvent();
		}
		
		override protected function keyUpHandler(event:KeyboardEvent):void
		{
			if (event.keyCode != Keyboard.SPACE)
			{
				return;
			}
			
			if (enabled && keyboardPressed)
			{
				// Mimic mouse click on the button.
				keyboardPressed = false;
				if (icon)
				{
					icon.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true));
				}
			}
			event.updateAfterEvent();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event listeners
		//
		//--------------------------------------------------------------------------
		
		private function icon_clickHandler(event:MouseEvent):void
		{
			selected = true;
			callback();
		}
	}
}